<template>
  <rx-dialog @handOk="handleSubmit" @cancel="cancel">
    <rx-layout>
      <div slot="center">
        <a-form  :form="form" class="editforms">
          <a-row>
            <a-col :span="24">
              <a-form-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('form.note','申请理由')">
                <a-textarea v-decorator="['note', {rules: [{required: true, message: i18n('form.note','申请理由','input')}]}]" :rows="8"/>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24">
              <a-form-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('form.orgChoice','机构选择')">
                <rx-grid
                  ref="table"
                  :columns="columns"
                  :dataSource="dataSource"
                  :allowRowSelect="true"
                  :multiSelect="true"
                  :showPage="false"
                  :bordered="true"
                  heightauto="true"
                  id-field="instId"
                >
                </rx-grid>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </rx-layout>
  </rx-dialog>
</template>
<script>
  import OsInstApi from "@/api/user/org/osInst";
  import {BaseForm, RxDialog, RxGrid, Util} from "@lowcode/jpaas-common-lib";
  import {mapState} from "vuex";
  export default {
    name: 'OsInstJoinInstEdit',
    mixins:[BaseForm],
    components: {
      RxDialog,
      RxGrid
    },
    data(){
      return {
        columns:[{title: this.i18n('form.nameCn','机构中文名'), dataIndex: 'nameCn', width: 100},
          {title: this.i18n('form.domain','机构域名'), dataIndex: 'domain', width: 100}],
        dataSource:[]
      }
    },
    computed:{
        ...mapState({
            //是否管理员
            isAdmin:state => state.appSetting.user.admin,
        })
    },
    methods: {
      i18n(name,text,key){
          return this.$ti18(name,text,"OsInstShowCanApply",key);
      },
      onload_(){
        OsInstApi.getList().then(result=>{
          this.dataSource = result.data;
        })
      },
      save(values){
        if(this.isAdmin){
            this.$message.error(this.i18n('msa.isAdminTips','管理员不能申请加入其他机构!'));
            return new Promise(resolve => {
                resolve({success:false});
            })

        }
        var data=[];
        var rows=this.$refs.table.getSelectedRows();
        if(rows.length==0){
          this.$message.error(this.i18n('msa.selectRows','请选择至少一个机构!'));
          return new Promise(resolve => {
            resolve({success:false});
          })
        }
        for(var i=0;i<rows.length;i++){
          data.push({instId:rows[i].instId,moreInstNote:values.note});
        }
        return OsInstApi.saveSelectInst(data);
      }
    }
  }
</script>